//
//  CTCertificationView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/28.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTCertificationView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "UIViewAdditions.h"
#import "CTSetView_Def.h"
#import "CTDeviceFunction.h"

#define CT_SETVIEW_BUTTON0 1000
#define CT_SETVIEW_BUTTON1 1001
#define CT_SETVIEW_BUTTON2 1002

static int distributeImageCount = 1;

@interface CTCertificationView ()<UITextFieldDelegate,UITextViewDelegate>
{
    UIView *topView;
    UIImageView *imageView1;
    UIImageView *imageView2;
    UIImageView *imageView3;
    UIImageView *imageView4;
    
    UIButton * addImgButton;
    
    UITextView  *reasonTextField;
    UITextField *relationTextField;
    UITextField *applyTextField;
}
@end


@implementation CTCertificationView
@synthesize delegate;

- (void)dealloc
{
    imageView4 = nil;
    imageView3 = nil;
    imageView2 = nil;
    imageView1 = nil;
    
    addImgButton = nil;
    
    reasonTextField = nil;
    relationTextField = nil;
    applyTextField = nil;
    
    delegate = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTCertificationView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.frame = CGRectMake(0, 0,CT_HEADER_SCREEN_WIDTH,CT_HEADER_SCREEN_HEIGHT);
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        [self createTitleView];
        [self createCertificationView];
    }
    return self;
}
- (void)createTitleView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_ORANGE_COLOR;
    [self addSubview:topView];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.font = [UIFont fontWithName:nil size:CT_HEADER_TITLE_FONTSIZE];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"certification_title_text" withValue:nil withTable:@"Language"];
    titleLabel.textColor = CT_HEADER_WHITE_COLOR;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
}
- (void)createCertificationView
{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-64)];
    scrollView.contentSize = CGSizeMake(CT_HEADER_SCREEN_WIDTH, 603);
    scrollView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:scrollView];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [scrollView addGestureRecognizer:tapGesture];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 25, CT_HEADER_SCREEN_WIDTH-20, 48)];
    label.text = [CTAppLanguage localizedStringForKey:@"certification_topLabel_text" withValue:nil withTable:@"Language"];
    label.font = [UIFont fontWithName:nil size:13];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = SET_CACHE_WORD_ORANGE_COLOR;
    label.lineBreakMode = NSLineBreakByCharWrapping;
    label.numberOfLines= 0;
    [scrollView addSubview:label];
    [self LabelHeightLabel:label fontSize:13];
    //申请称号
    UILabel *applyLabel = [[UILabel alloc]initWithFrame:CGRectMake(13,label.bottom+44 , 75, 15)];
    applyLabel.textColor = SET_LABEL_TEXT_COLOR;
    applyLabel.text = [CTAppLanguage localizedStringForKey:@"certification_apply_text" withValue:nil withTable:@"Language"];
    applyLabel.font = [UIFont fontWithName:nil size:15];
    applyLabel.textAlignment = NSTextAlignmentLeft;
    [scrollView addSubview:applyLabel];
    UIImageView *applyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(applyLabel.right, label.bottom+30, CT_HEADER_SCREEN_WIDTH-100, 40)];
    applyImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [scrollView addSubview:applyImageView];
    applyTextField = [[UITextField alloc]initWithFrame:CGRectMake(applyImageView.origin.x+10, applyImageView.origin.y, applyImageView.width-20, applyImageView.height)];
    applyTextField.placeholder = [CTAppLanguage localizedStringForKey:@"certification_applyP_text" withValue:nil withTable:@"Language"];
    applyTextField.delegate = self;
    applyTextField.tag = CT_SETVIEW_BUTTON0;
    applyTextField.textAlignment = NSTextAlignmentLeft;
    applyTextField.font = [UIFont fontWithName:nil size:15];
    applyTextField.textColor = SET_CACHE_WORD_ORANGE_COLOR;
    [scrollView addSubview:applyTextField];
    //联系方式
    UILabel *relationLabel = [[UILabel alloc]initWithFrame:CGRectMake(13,applyLabel.bottom+46 , 75, 15)];
    relationLabel.textColor = SET_LABEL_TEXT_COLOR;
    relationLabel.text = [CTAppLanguage localizedStringForKey:@"certification_relation_text" withValue:nil withTable:@"Language"];
    relationLabel.font = [UIFont fontWithName:nil size:15];
    relationLabel.textAlignment = NSTextAlignmentLeft;
    [scrollView addSubview:relationLabel];
    UIImageView *relationImageView = [[UIImageView alloc]initWithFrame:CGRectMake(applyLabel.right, applyImageView.bottom+20, applyImageView.width, 40)];
    relationImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [scrollView addSubview:relationImageView];
    relationTextField = [[UITextField alloc]initWithFrame:CGRectMake(relationImageView.origin.x+10, relationImageView.origin.y, relationImageView.width-20, relationImageView.height)];
    relationTextField.placeholder = [CTAppLanguage localizedStringForKey:@"certification_relationP_text" withValue:nil withTable:@"Language"];
    relationTextField.font = [UIFont fontWithName:nil size:15];
    relationTextField.delegate = self;
    relationTextField.tag = CT_SETVIEW_BUTTON1;
    relationTextField.textAlignment = NSTextAlignmentLeft;
    relationTextField.textColor = SET_CACHE_WORD_ORANGE_COLOR;
    [scrollView addSubview:relationTextField];
    
    //申请理由
    UILabel *reasonLabel = [[UILabel alloc]initWithFrame:CGRectMake(13,relationLabel.bottom+46 , 75, 15)];
    reasonLabel.textColor = SET_LABEL_TEXT_COLOR;
    reasonLabel.text = [CTAppLanguage localizedStringForKey:@"certification_reason_text" withValue:nil withTable:@"Language"];
    reasonLabel.font = [UIFont fontWithName:nil size:15];
    reasonLabel.textAlignment = NSTextAlignmentLeft;
    [scrollView addSubview:reasonLabel];
    UIImageView *reasonImageView = [[UIImageView alloc]initWithFrame:CGRectMake(reasonLabel.right, relationImageView.bottom+20, applyImageView.width, 160)];
    reasonImageView.backgroundColor = [UIColor redColor];
    reasonImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [scrollView addSubview:reasonImageView];
    reasonTextField = [[UITextView alloc]initWithFrame:CGRectMake(reasonImageView.origin.x+0.5, reasonImageView.origin.y+0.5, reasonImageView.width-1, reasonImageView.height-1)];
    reasonTextField.text = [CTAppLanguage localizedStringForKey:@"certification_reasonP_text" withValue:nil withTable:@"Language"];
    reasonTextField.delegate = self;
    reasonTextField.tag = CT_SETVIEW_BUTTON2;
    reasonTextField.font = [UIFont fontWithName:nil size:15];
    reasonTextField.backgroundColor = CT_HEADER_WHITE_COLOR;
    reasonTextField.textAlignment = NSTextAlignmentLeft;
    reasonTextField.textColor = SET_CACHE_WORD_ORANGE_COLOR;
    [scrollView addSubview:reasonTextField];
    
    imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, reasonTextField.bottom+30, (CT_HEADER_SCREEN_WIDTH-76)/4,(CT_HEADER_SCREEN_WIDTH-76)/4)];
    [scrollView addSubview:imageView1];
    addImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addImgButton addTarget:self action:@selector(addImgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    addImgButton.frame = CGRectMake(imageView1.origin.x, imageView1.origin.y, imageView1.width, imageView1.height);
    [addImgButton setBackgroundImage:[UIImage imageNamed:@"addpic"] forState:UIControlStateNormal];
    [addImgButton setBackgroundImage:[UIImage imageNamed:@"addpic_click"] forState:UIControlStateHighlighted];
    [scrollView addSubview:addImgButton];
    
    imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(imageView1.right+18, imageView1.origin.y, imageView1.width, imageView1.height)];
    [scrollView addSubview:imageView2];
    
    imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(imageView2.right+18, imageView1.origin.y, imageView1.width, imageView1.height)];
    [scrollView addSubview:imageView3];
    
    imageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(imageView3.right+18, imageView1.origin.y, imageView1.width, imageView1.height)];
    [scrollView addSubview:imageView4];
    
    
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    DeviceTypeAdaptation deviceType = [CTDeviceFunction deviceTypeForResolutionAdaptation];
    submitBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-305)/2, scrollView.contentSize.height-58, 305, 38);
    submitBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-305)/2, scrollView.contentSize.height-58, 305, 38);
    [submitBtn setTitle:[CTAppLanguage localizedStringForKey:@"certification_submit_text" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [submitBtn setTitleColor:SET_WORD_ORANGE_COLOR forState:UIControlStateNormal];
    [submitBtn setTitleColor:SET_WORD_CLICK_ORANGE_COLOR forState:UIControlStateHighlighted];
    [submitBtn addTarget:self action:@selector(submitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn setBackgroundImage:[UIImage imageNamed:@"signup"] forState:UIControlStateNormal];
    [submitBtn setBackgroundImage:[UIImage imageNamed:@"signup_click"] forState:UIControlStateHighlighted];
    if (deviceType == iPhone6_Plus)
    {
        submitBtn.frame = CGRectMake((CT_HEADER_SCREEN_WIDTH-305)/2, CT_HEADER_SCREEN_HEIGHT-58, 305, 38);
        [self addSubview:submitBtn];
    }
    else
    {
        
        [scrollView addSubview:submitBtn];
    }
    
    
}
//lable自适应高度
- (void)LabelHeightLabel:(UILabel*)label fontSize:(float)fontSize
{
    
    CGSize  titleSize = [label.text sizeWithFont:[UIFont systemFontOfSize:fontSize]
                               constrainedToSize:CGSizeMake(CT_HEADER_SCREEN_WIDTH-20, 200)
                                   lineBreakMode:NSLineBreakByCharWrapping];
    label.frame = CGRectMake(label.frame.origin.x,
                             label.frame.origin.y,
                             label.frame.size.width,
                             titleSize.height);
}
//回调相册
- (void)addImgButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(certificationAddImageBtnAction:)])
    {
        [self.delegate certificationAddImageBtnAction:self];
    }
}
//设置图片
-(void)setDistributeImage:(UIImage *)img
{
    if (img == nil)return;
    
    switch (distributeImageCount)
    {
        case 1:
            imageView2.image = img;
            break;
        case 2:
            imageView3.image = img;
            break;
        case 3:
            imageView4.image = img;
            break;
        case 4:
            imageView1.image = imageView2.image;
            imageView2.image = imageView3.image;
            imageView3.image = imageView4.image;
            imageView4.image = img;
            
            [addImgButton removeFromSuperview];
            break;
            
        default:
            break;
    }
    distributeImageCount ++;
}

//提交按钮
- (void)submitBtnAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(certificationSubmitButton:)])
    {
        [self.delegate certificationSubmitButton:self];
    }
}
//返回按钮
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(certificationBackButton:)])
    {
        [self.delegate certificationBackButton:self];
    }
}
#pragma mark textView Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.text = nil;
}
-(void)textViewDidChange:(UITextView *)textView
{
    if ([self.delegate respondsToSelector:@selector(certificationReasonTextFieldText:withView:)])
    {
        [self.delegate certificationReasonTextFieldText:textView.text withView:self];
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length <1)
    {
        textView.text =   [CTAppLanguage localizedStringForKey:@"certification_reasonP_text" withValue:nil withTable:@"Language"];
    }
}
#pragma mark textField Delegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag)
    {
        case CT_SETVIEW_BUTTON0:
            if ([self.delegate respondsToSelector:@selector(certificationNameTextFieldText:withView:)])
            {
                [self.delegate certificationNameTextFieldText:textField.text withView:self];
            }
            break;
        case CT_SETVIEW_BUTTON1:
            if ([self.delegate respondsToSelector:@selector(certificationPhoneTextFieldText:withView:)])
            {
                [self.delegate certificationPhoneTextFieldText:textField.text withView:self];
            }
            break;
        default:
            break;
    }
}

//回收键盘
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [relationTextField resignFirstResponder];
    [applyTextField resignFirstResponder];
    [reasonTextField resignFirstResponder];
}
@end
